package cn.winwintech.Incense.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Service;

import com.base.webapp.respm.ResException;
import com.base.webapp.respm.WeatherInfo;
import com.base.webapp.util.StringUtil;

import cn.winwintech.Incense.util.MobClient;
import cn.winwintech.Incense.util.WeatherHelper;

@Service
public class WeatherService extends ServiceBase {
	private static final int FIFTENN_MINUTE = 15 * 60 * 1000;
	public static final String appId = "20244afb6c480186";
	public static final String appIdSix = appId.substring(0, 6);
	public static final String privateKey = "a32f71_SmartWeatherAPI_e91aef5";
	public static final String KEY = "3d4f765b0147802cb906312d6f1fab13";
	public static String cache = null;
	public static Long lastUpdate = System.currentTimeMillis();

	public String getForecast(String areaId) throws Exception {
		if (cache != null && lastUpdate + FIFTENN_MINUTE > System.currentTimeMillis()) {
			return cache;
		}
		String result = fetchInfoFromOtherServer(areaId);
		String result2 = fetchInfo(areaId);
		if (result != null && !result.contains("error")) {
			if (isForecastValid(result, result2)) {
				cache = result;
				lastUpdate = System.currentTimeMillis();
			}
		}
		return cache;
	}

	private boolean isForecastValid(String result, String result2) throws ResException {
		WeatherInfo info = new WeatherInfo(result, result2);
		if (!StringUtil.isBlank(info.weather)) {
			return true;
		}
		return false;
	}

	public String fetchInfo(String areaId) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {

			// 
			
			String url = "http://www.weather.com.cn/adat/sk/" + areaId + ".html";
			HttpGet httpGet = new HttpGet(url);
			CloseableHttpResponse response = httpclient.execute(httpGet);
			String result = StringUtil.parsRtn(response.getEntity().getContent());
			httpclient.close();
			System.out.println(result);
			return result;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;

	}

	public static String fetchInfoFromOtherServer(String areaId) {
		MobClient client = null;
		try {
			String type = "forecast_v";
			String date = new SimpleDateFormat("yyyyMMddHHmm").format(new Date(System.currentTimeMillis()));
			String publicKey = "http://open.weather.com.cn/data/?areaid=" + areaId + "&type=" + type + "&date=" + date
					+ "&appid=" + appId;
			String key = WeatherHelper.standardURLEncoder(publicKey, privateKey);
			String url = "http://open.weather.com.cn/data/?areaid=" + areaId + "&type=" + type + "&date=" + date
					+ "&appid=" + appIdSix + "&key=" + key;
			client = new MobClient(url, MobClient.REQ_METHOD_GET);
			String result = client.connect();
			return result;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			client.release();
		}
		return null;

	}
}
